package com.us.usws.common.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.views.jsp.StrutsBodyTagSupport;

import com.us.usws.model.domain.base.VideoTypeExtattr;

/**
 *
 * @author Wang Jianbo
 */
public class VideoTypeExtattrInputTag extends StrutsBodyTagSupport {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getFactory().getInstance(VideoTypeExtattrInputTag.class);

	private String value;
	private String recMap;
	private VideoTypeExtattr extatt;

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Override
	public int doStartTag() throws JspException {
		extatt = (VideoTypeExtattr) findValue(value);
		StringBuilder sb = new StringBuilder();
		String itype = extatt.getInputType();
		log.info("In tag, find input=[" + itype + "], while seqNum=[" + extatt.getSeqNum() + "].");

		switch (extatt.getInputType()) {
		case "checkbox":
			String[] cvals = StringUtils.split(extatt.getValueOptions(), ',');
			String cdftVal = extatt.getDefaultValue();

			if (extatt.getShowPrefix() != null) {
				sb.append(extatt.getShowPrefix());
			}
			for (String each : cvals) {
				each = each.trim();
				String eachVal = StringEscapeUtils.escapeHtml4(each);
				sb.append("<input type=\"checkbox\" name=\"").append(recMap).append(".vte").append(extatt.getUid()).append("\" value=\"");
				sb.append(eachVal).append("\"");
				if (each.equals(cdftVal)) {
					sb.append(" checked=\"checked\"");
				}
				sb.append(">").append(eachVal).append("&nbsp;&nbsp;\n");

			}
			if (extatt.getShowSuffix() != null) {
				sb.append(extatt.getShowSuffix());
			}
			break;

		case "radio":
			String[] rvals = StringUtils.split(extatt.getValueOptions(), ',');
			String rdftVal = extatt.getDefaultValue();
			if (StringUtils.isBlank(rdftVal)) {
				rdftVal = rvals[0].trim();
			}
			if (extatt.getShowPrefix() != null) {
				sb.append(extatt.getShowPrefix());
			}
			for (String each : rvals) {
				each = each.trim();
				String eachVal = StringEscapeUtils.escapeHtml4(each);
				sb.append("<input type=\"radio\" name=\"").append(recMap).append(".vte").append(extatt.getUid()).append("\" value=\"");
				sb.append(eachVal).append("\"");
				if (each.equals(rdftVal)) {
					sb.append(" checked=\"checked\"");
				}
				sb.append(">").append(eachVal).append("&nbsp;&nbsp;\n");

			}
			if (extatt.getShowSuffix() != null) {
				sb.append(extatt.getShowSuffix());
			}
			break;

		default:
			if (extatt.getShowPrefix() != null) {
				sb.append(extatt.getShowPrefix());
			}

			sb.append("<input type=\"text\" name=\"").append(recMap).append(".vte").append(extatt.getUid()).append("\">");

			if (extatt.getShowSuffix() != null) {
				sb.append(extatt.getShowSuffix());
			}
		}

		//write
		JspWriter writer = pageContext.getOut();
		try {
			writer.print(sb.toString());
			//pageContext.getResponse().getWriter().println(content);
		} catch (IOException e) {
			log.error("IOException occurs", e);
			throw new JspException(e.toString());
		}

		return SKIP_BODY;
	}

	public String getRecMap() {
		return recMap;
	}

	public void setRecMap(String recMap) {
		this.recMap = recMap;
	}

}
